Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pg/catalog/alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ func (c *Catalog) ExecRenameStmt(stmt *nodes.RenameStmt) error {
return c.renameSequence(stmt)
case nodes.OBJECT_TRIGGER:
return c.renameTrigger(stmt)
case nodes.OBJECT_EVENT_TRIGGER:
return c.renameEventTrigger(stmt)
case nodes.OBJECT_TABCONSTRAINT:
return c.renameConstraint(stmt)
case nodes.OBJECT_FUNCTION, nodes.OBJECT_PROCEDURE, nodes.OBJECT_ROUTINE:
Expand Down
60 changes: 33 additions & 27 deletions pg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ type Catalog struct {
relationByOID map[uint32]*Relation

// Constraint indexes.
constraints map[uint32]*Constraint // OID → Constraint
constraints map[uint32]*Constraint // OID → Constraint
consByRel map[uint32][]*Constraint // relOID → constraints

// Index indexes.
indexes map[uint32]*Index // OID → Index
indexes map[uint32]*Index // OID → Index
indexesByRel map[uint32][]*Index // relOID → indexes

// Sequence index.
Expand All @@ -76,6 +76,10 @@ type Catalog struct {
triggers map[uint32]*Trigger
triggersByRel map[uint32][]*Trigger

// Event triggers.
eventTriggers map[uint32]*EventTrigger
eventTriggerByName map[string]*EventTrigger

// Comments.
comments map[commentKey]string

Expand Down Expand Up @@ -130,31 +134,33 @@ type Catalog struct {
// New creates a fully initialized Catalog with all built-in data indexed.
func New() *Catalog {
c := &Catalog{
oidGen: NewOIDGenerator(),
schemas: make(map[uint32]*Schema),
schemaByName: make(map[string]*Schema),
typeByOID: make(map[uint32]*BuiltinType, len(BuiltinTypes)),
typeByName: make(map[typeKey]*BuiltinType, len(BuiltinTypes)),
castIndex: make(map[castKey]*BuiltinCast, len(BuiltinCasts)),
operByOID: make(map[uint32]*BuiltinOperator, len(BuiltinOperators)),
operByKey: make(map[operKey][]*BuiltinOperator, len(BuiltinOperators)),
procByOID: make(map[uint32]*BuiltinProc, len(BuiltinProcs)),
procByName: make(map[string][]*BuiltinProc),
relationByOID: make(map[uint32]*Relation),
constraints: make(map[uint32]*Constraint),
consByRel: make(map[uint32][]*Constraint),
indexes: make(map[uint32]*Index),
indexesByRel: make(map[uint32][]*Index),
sequenceByOID: make(map[uint32]*Sequence),
enumTypes: make(map[uint32]*EnumType),
domainTypes: make(map[uint32]*DomainType),
rangeTypes: make(map[uint32]*RangeType),
userProcs: make(map[uint32]*UserProc),
triggers: make(map[uint32]*Trigger),
triggersByRel: make(map[uint32][]*Trigger),
comments: make(map[commentKey]string),
policies: make(map[uint32]*Policy),
policiesByRel: make(map[uint32][]*Policy),
oidGen: NewOIDGenerator(),
schemas: make(map[uint32]*Schema),
schemaByName: make(map[string]*Schema),
typeByOID: make(map[uint32]*BuiltinType, len(BuiltinTypes)),
typeByName: make(map[typeKey]*BuiltinType, len(BuiltinTypes)),
castIndex: make(map[castKey]*BuiltinCast, len(BuiltinCasts)),
operByOID: make(map[uint32]*BuiltinOperator, len(BuiltinOperators)),
operByKey: make(map[operKey][]*BuiltinOperator, len(BuiltinOperators)),
procByOID: make(map[uint32]*BuiltinProc, len(BuiltinProcs)),
procByName: make(map[string][]*BuiltinProc),
relationByOID: make(map[uint32]*Relation),
constraints: make(map[uint32]*Constraint),
consByRel: make(map[uint32][]*Constraint),
indexes: make(map[uint32]*Index),
indexesByRel: make(map[uint32][]*Index),
sequenceByOID: make(map[uint32]*Sequence),
enumTypes: make(map[uint32]*EnumType),
domainTypes: make(map[uint32]*DomainType),
rangeTypes: make(map[uint32]*RangeType),
userProcs: make(map[uint32]*UserProc),
triggers: make(map[uint32]*Trigger),
triggersByRel: make(map[uint32][]*Trigger),
eventTriggers: make(map[uint32]*EventTrigger),
eventTriggerByName: make(map[string]*EventTrigger),
comments: make(map[commentKey]string),
policies: make(map[uint32]*Policy),
policiesByRel: make(map[uint32][]*Policy),
extensions: make(map[uint32]*Extension),
extByName: make(map[string]*Extension),
accessMethods: make(map[uint32]*AccessMethod),
Expand Down
13 changes: 13 additions & 0 deletions pg/catalog/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ func (c *Catalog) Clone() *Catalog {
clone.triggersByRel[ct.RelOID] = append(clone.triggersByRel[ct.RelOID], &ct)
}

// --- Event triggers: deep copy + rebuild name index ---
clone.eventTriggers = make(map[uint32]*EventTrigger, len(c.eventTriggers))
clone.eventTriggerByName = make(map[string]*EventTrigger, len(c.eventTriggerByName))
for oid, evt := range c.eventTriggers {
ce := *evt
if evt.Tags != nil {
ce.Tags = make([]string, len(evt.Tags))
copy(ce.Tags, evt.Tags)
}
clone.eventTriggers[oid] = &ce
clone.eventTriggerByName[ce.Name] = &ce
}

// --- Comments: copy map ---
clone.comments = make(map[commentKey]string, len(c.comments))
for k, v := range c.comments {
Expand Down
10 changes: 8 additions & 2 deletions pg/catalog/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ func (c *Catalog) CommentObject(stmt *nodes.CommentStmt) error {
return &Error{Code: CodeUndefinedObject, Message: fmt.Sprintf("trigger %q does not exist", trigName)}
}

case nodes.OBJECT_EVENT_TRIGGER:
name := extractSimpleObjectName(stmt.Object)
evt := c.eventTriggerByName[name]
if evt == nil {
return errUndefinedObject("event trigger", name)
}
key = commentKey{ObjType: 'E', ObjOID: evt.OID}

case nodes.OBJECT_COLLATION,
nodes.OBJECT_CONVERSION,
nodes.OBJECT_OPERATOR,
Expand All @@ -301,7 +309,6 @@ func (c *Catalog) CommentObject(stmt *nodes.CommentStmt) error {
nodes.OBJECT_EXTENSION,
nodes.OBJECT_ACCESS_METHOD,
nodes.OBJECT_PUBLICATION,
nodes.OBJECT_EVENT_TRIGGER,
nodes.OBJECT_DATABASE,
nodes.OBJECT_TABLESPACE,
nodes.OBJECT_ROLE,
Expand Down Expand Up @@ -361,4 +368,3 @@ func (c *Catalog) removeComments(objType byte, objOID uint32) {
}
}
}

6 changes: 6 additions & 0 deletions pg/catalog/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,12 @@ func (c *Catalog) dropDependents(refType byte, refOID uint32) {
continue
}
c.removeTrigger(trig)
case 'E':
evt := c.eventTriggers[dep.ObjOID]
if evt == nil {
continue
}
c.removeEventTrigger(evt)
}
}
}
Expand Down
35 changes: 23 additions & 12 deletions pg/catalog/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ type SchemaDiffEntry struct {

// RelationDiffEntry describes a relation (table/view/matview) change.
type RelationDiffEntry struct {
Action DiffAction
SchemaName string
Name string
From *Relation
To *Relation
Columns []ColumnDiffEntry
Constraints []ConstraintDiffEntry
Indexes []IndexDiffEntry
Triggers []TriggerDiffEntry
Policies []PolicyDiffEntry
RLSChanged bool
RLSEnabled bool
Action DiffAction
SchemaName string
Name string
From *Relation
To *Relation
Columns []ColumnDiffEntry
Constraints []ConstraintDiffEntry
Indexes []IndexDiffEntry
Triggers []TriggerDiffEntry
Policies []PolicyDiffEntry
RLSChanged bool
RLSEnabled bool
ForceRLSEnabled bool
}

Expand Down Expand Up @@ -92,6 +92,14 @@ type TriggerDiffEntry struct {
To *Trigger
}

// EventTriggerDiffEntry describes a database-level event trigger change.
type EventTriggerDiffEntry struct {
Action DiffAction
Name string
From *EventTrigger
To *EventTrigger
}

// EnumDiffEntry describes an enum type change.
type EnumDiffEntry struct {
Action DiffAction
Expand Down Expand Up @@ -176,6 +184,7 @@ type SchemaDiff struct {
Ranges []RangeDiffEntry
CompositeTypes []CompositeTypeDiffEntry
Extensions []ExtensionDiffEntry
EventTriggers []EventTriggerDiffEntry
Comments []CommentDiffEntry
Grants []GrantDiffEntry
}
Expand All @@ -191,6 +200,7 @@ func (d *SchemaDiff) IsEmpty() bool {
len(d.Ranges) == 0 &&
len(d.CompositeTypes) == 0 &&
len(d.Extensions) == 0 &&
len(d.EventTriggers) == 0 &&
len(d.Comments) == 0 &&
len(d.Grants) == 0
}
Expand All @@ -210,6 +220,7 @@ func Diff(from, to *Catalog) *SchemaDiff {
Ranges: diffRanges(from, to),
CompositeTypes: diffComposites(from, to),
Extensions: diffExtensions(from, to),
EventTriggers: diffEventTriggers(from, to),
Comments: diffComments(from, to),
Grants: diffGrants(from, to),
}
Expand Down
8 changes: 7 additions & 1 deletion pg/catalog/diff_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ func resolveCommentDescription(c *Catalog, ck commentKey) string {
}
return fmt.Sprintf("%s.%s.%s", rel.Schema.Name, rel.Name, trig.Name)

case 'E': // event trigger
evt := c.eventTriggers[ck.ObjOID]
if evt == nil {
return ""
}
return evt.Name

case 'd': // domain constraint
// Domain constraints are stored on DomainType.Constraints.
for _, dt := range c.domainTypes {
Expand Down Expand Up @@ -222,4 +229,3 @@ func resolveCommentDescription(c *Catalog, ck commentKey) string {
return ""
}
}

3 changes: 2 additions & 1 deletion pg/catalog/dropcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (c *Catalog) RemoveObjects(stmt *nodes.DropStmt) error {
return c.removeFunctionObjects(stmt)
case nodes.OBJECT_TRIGGER:
return c.removeTriggerObjects(stmt)
case nodes.OBJECT_EVENT_TRIGGER:
return c.removeEventTriggerObjects(stmt)
case nodes.OBJECT_POLICY:
return c.removePolicyObjects(stmt)
case nodes.OBJECT_TABLE, nodes.OBJECT_VIEW, nodes.OBJECT_MATVIEW:
Expand All @@ -53,7 +55,6 @@ func (c *Catalog) RemoveObjects(stmt *nodes.DropStmt) error {
nodes.OBJECT_FOREIGN_SERVER,
nodes.OBJECT_ACCESS_METHOD,
nodes.OBJECT_PUBLICATION,
nodes.OBJECT_EVENT_TRIGGER,
nodes.OBJECT_RULE,
nodes.OBJECT_CAST,
nodes.OBJECT_TRANSFORM,
Expand Down
Loading
Loading