Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for ALTER/DROP SUSCRIPTION #253

Merged
merged 14 commits into from
Dec 7, 2024

Conversation

TianyuZhang1214
Copy link
Contributor

@TianyuZhang1214 TianyuZhang1214 commented Dec 4, 2024

Resolve #246

New Features

  • ALTER SUBSCRIPTION:
    Added support for ALTER SUBSCRIPTION mysub [ENABLE | DISABLE].

    • DISABLE: Pauses replication from the source PostgreSQL.
    • ENABLE: Resumes replication from the source PostgreSQL.
  • DROP SUBSCRIPTION:
    Added support for DROP SUBSCRIPTION mysub.
    Note: Dropping a subscription does not remove any previously replicated data.

Improvements

  • Introduced a subscriptionMap in subscription.go to manage all subscriptions centrally.
  • Merged system table __sys__.pg_replication_lsn into __sys__.pg_subscription.

@TianyuZhang1214 TianyuZhang1214 linked an issue Dec 4, 2024 that may be closed by this pull request
@TianyuZhang1214 TianyuZhang1214 marked this pull request as ready for review December 6, 2024 03:59
ValueColumns: []string{"connection", "publication"},
DDL: "name TEXT PRIMARY KEY, connection TEXT, publication TEXT",
KeyColumns: []string{"subname"},
ValueColumns: []string{"subconninfo", "subpublication", "subskiplsn", "subenabled"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sub prefix for all column names can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sub is chosen to maintain consistency with the pg_subscription catalog in PostgreSQL.

@@ -1059,15 +1059,15 @@ func (h *ConnectionHandler) handledPSQLCommands(statement string) (bool, error)
statement = strings.ToLower(statement)
// Command: \l
if statement == "select d.datname as \"name\",\n pg_catalog.pg_get_userbyid(d.datdba) as \"owner\",\n pg_catalog.pg_encoding_to_char(d.encoding) as \"encoding\",\n d.datcollate as \"collate\",\n d.datctype as \"ctype\",\n d.daticulocale as \"icu locale\",\n case d.datlocprovider when 'c' then 'libc' when 'i' then 'icu' end as \"locale provider\",\n pg_catalog.array_to_string(d.datacl, e'\\n') as \"access privileges\"\nfrom pg_catalog.pg_database d\norder by 1;" {
query, err := h.convertQuery(`select d.datname as "Name", 'postgres' as "Owner", 'UTF8' as "Encoding", 'en_US.UTF-8' as "Collate", 'en_US.UTF-8' as "Ctype", 'en-US' as "ICU Locale", case d.datlocprovider when 'c' then 'libc' when 'i' then 'icu' end as "locale provider", '' as "access privileges" from pg_catalog.pg_database d order by 1;`)
query, err := h.convertQuery(`select d.datname as "Subscription", 'postgres' as "Owner", 'UTF8' as "Encoding", 'en_US.UTF-8' as "Collate", 'en_US.UTF-8' as "Ctype", 'en-US' as "ICU Locale", case d.datlocprovider when 'c' then 'libc' when 'i' then 'icu' end as "locale provider", '' as "access privileges" from pg_catalog.pg_database d order by 1;`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Enabled: enabled,
Replicator: nil,
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check rows.Err() after the for loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

var lsnValueColumns = []string{"subskiplsn"}

var subscriptionMap = sync.Map{}
var mu sync.Mutex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary and dangerous global mutex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
defer rows.Close()

if !rows.Next() {
return
var tempMap = make(map[string]*Subscription)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temp prefix is unnecessary. It is a local variable, implying it is temporary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

subscriptionName := subscriptionMatch[1]
connectionString := subscriptionMatch[2]
publicationName := subscriptionMatch[3]
func (h *ConnectionHandler) executeAlterEnable(subscriptionConfig *SubscriptionConfig) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executeAlterEnable -> executeEnableSubscription(config *SubscriptionConfig)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return commitAndUpdate(sqlCtx)
}

func (h *ConnectionHandler) executeAlterDisable(subscriptionConfig *SubscriptionConfig) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> executeDisableSubscription(config *SubscriptionConfig)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

@fanyang01 fanyang01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks.

@TianyuZhang1214 TianyuZhang1214 merged commit 3c612a8 into main Dec 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for ALTER SUBSCRIPTION mysub ENABLE/DISABLE
2 participants