MCPcopy Create free account
hub / github.com/apache/cloudberry / validateForeignKeyConstraint

Function validateForeignKeyConstraint

src/backend/commands/tablecmds.c:13167–13263  ·  view source on GitHub ↗

* Scan the existing rows in a table to verify they meet a proposed FK * constraint. * * Caller must have opened and locked both relations appropriately. */

Source from the content-addressed store, hash-verified

13165 * Caller must have opened and locked both relations appropriately.
13166 */
13167static void
13168validateForeignKeyConstraint(char *conname,
13169 Relation rel,
13170 Relation pkrel,
13171 Oid pkindOid,
13172 Oid constraintOid)
13173{
13174 TupleTableSlot *slot;
13175 TableScanDesc scan;
13176 Trigger trig;
13177 Snapshot snapshot;
13178 MemoryContext oldcxt;
13179 MemoryContext perTupCxt;
13180
13181 ereport(DEBUG1,
13182 (errmsg_internal("validating foreign key constraint \"%s\"", conname)));
13183
13184 /* Apache Cloudberry: Ignore foreign keys for now, with a warning. */
13185 if (Gp_role == GP_ROLE_DISPATCH || Gp_role == GP_ROLE_UTILITY)
13186 ereport(WARNING,
13187 (errcode(ERRCODE_GP_FEATURE_NOT_YET),
13188 errmsg("referential integrity (FOREIGN KEY) constraints are not supported in Apache Cloudberry, will not be enforced")));
13189 return;
13190
13191 /*
13192 * Build a trigger call structure; we'll need it either way.
13193 */
13194 MemSet(&trig, 0, sizeof(trig));
13195 trig.tgoid = InvalidOid;
13196 trig.tgname = conname;
13197 trig.tgenabled = TRIGGER_FIRES_ON_ORIGIN;
13198 trig.tgisinternal = true;
13199 trig.tgconstrrelid = RelationGetRelid(pkrel);
13200 trig.tgconstrindid = pkindOid;
13201 trig.tgconstraint = constraintOid;
13202 trig.tgdeferrable = false;
13203 trig.tginitdeferred = false;
13204 /* we needn't fill in remaining fields */
13205
13206 /*
13207 * See if we can do it with a single LEFT JOIN query. A false result
13208 * indicates we must proceed with the fire-the-trigger method.
13209 */
13210 if (RI_Initial_Check(&trig, rel, pkrel))
13211 return;
13212
13213 /*
13214 * Scan through each tuple, calling RI_FKey_check_ins (insert trigger) as
13215 * if that tuple had just been inserted. If any of those fail, it should
13216 * ereport(ERROR) and that's that.
13217 */
13218 snapshot = RegisterSnapshot(GetLatestSnapshot());
13219 slot = table_slot_create(rel, NULL);
13220 scan = table_beginscan(rel, snapshot, 0, NULL);
13221
13222 perTupCxt = AllocSetContextCreate(CurrentMemoryContext,
13223 "validateForeignKeyConstraint",
13224 ALLOCSET_SMALL_SIZES);

Callers 1

ATRewriteTablesFunction · 0.85

Calls 15

RI_Initial_CheckFunction · 0.85
RegisterSnapshotFunction · 0.85
GetLatestSnapshotFunction · 0.85
table_slot_createFunction · 0.85
table_beginscanFunction · 0.85
MemoryContextSwitchToFunction · 0.85
table_scan_getnextslotFunction · 0.85
ExecFetchSlotHeapTupleFunction · 0.85
RI_FKey_check_insFunction · 0.85
MemoryContextResetFunction · 0.85
table_endscanFunction · 0.85
UnregisterSnapshotFunction · 0.85

Tested by

no test coverage detected