Skip to content

Commit

Permalink
Replace postgresql 14+ function with postgresql 12 compatible function (
Browse files Browse the repository at this point in the history
#12154)

* Replace postgresql 14+ function with postgresql 11 compatible function

* Capitalize sql keywords

* Update to postgresql 12, add note about size of multicolumn index
  • Loading branch information
reductionista authored Feb 28, 2024
1 parent e37f7e2 commit e9a2dd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (o *DbORM) InsertFilter(filter Filter, qopts ...pg.QOpt) (err error) {
(SELECT unnest(:address_array ::::BYTEA[]) addr) a,
(SELECT unnest(:event_sig_array ::::BYTEA[]) ev) e
%s
ON CONFLICT (hash_record_extended((name, evm_chain_id, address, event, topic2, topic3, topic4), 0))
ON CONFLICT (evm.f_log_poller_filter_hash(name, evm_chain_id, address, event, topic2, topic3, topic4))
DO UPDATE SET retention=:retention ::::BIGINT, max_logs_kept=:max_logs_kept ::::NUMERIC, logs_per_block=:logs_per_block ::::NUMERIC`,
topicsColumns.String(),
topicsSql.String())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
-- +goose Up

-- This generates a unique BIGINT for the log_poller_filters table from hashing (name, evm_chain_id, address, event, topic2, topic3, topic4).
-- Using an ordinary multi-column index on 7 columns would require a lot of extra storage space, and there are additional complications due to the topics being allowed to be NULL.
-- Note for updating this if and when we drop support for postgresql 12 & 13: hashrecordextended() can be used directly in postgresql 14, avoiding the need for a helper function.
-- The helper function is necessary only for the IMMUTABLE keyword
CREATE OR REPLACE FUNCTION evm.f_log_poller_filter_hash(name TEXT, evm_chain_id NUMERIC, address BYTEA, event BYTEA, topic2 BYTEA, topic3 BYTEA, topic4 BYTEA)
RETURNS BIGINT
LANGUAGE SQL IMMUTABLE COST 25 PARALLEL SAFE AS 'SELECT hashtextextended(textin(record_out(($1,$2,$3,$4,$5,$6,$7))), 0)';

ALTER TABLE evm.log_poller_filters
ADD COLUMN topic2 BYTEA CHECK (octet_length(topic2) = 32),
ADD COLUMN topic3 BYTEA CHECK (octet_length(topic3) = 32),
ADD COLUMN topic4 BYTEA CHECK (octet_length(topic4) = 32),
ADD COLUMN max_logs_kept BIGINT,
ADD COLUMN logs_per_block BIGINT;

-- Ordinary UNIQUE CONSTRAINT can't work for topics because they can be NULL. Any row with any column being NULL automatically satisfies the unique constraint (NULL != NULL)
-- Using a hash of all the columns treats NULL's as the same as any other field. If we ever get to a point where we can require postgresql >= 15 then this can
-- be fixed by using UNIQUE CONSTRAINT NULLS NOT DISTINCT which treats NULL's as if they were ordinary values (NULL == NULL)
CREATE UNIQUE INDEX evm_log_poller_filters_name_chain_address_event_topics_key ON evm.log_poller_filters (hash_record_extended((name, evm_chain_id, address, event, topic2, topic3, topic4), 0));
CREATE UNIQUE INDEX log_poller_filters_hash_key ON evm.log_poller_filters (evm.f_log_poller_filter_hash(name, evm_chain_id, address, event, topic2, topic3, topic4));

ALTER TABLE evm.log_poller_filters
DROP CONSTRAINT evm_log_poller_filters_name_evm_chain_id_address_event_key;
Expand All @@ -19,11 +24,13 @@ ALTER TABLE evm.log_poller_filters

ALTER TABLE evm.log_poller_filters
ADD CONSTRAINT evm_log_poller_filters_name_evm_chain_id_address_event_key UNIQUE (name, evm_chain_id, address, event);
DROP INDEX IF EXISTS evm_log_poller_filters_name_chain_address_event_topics_key;
DROP INDEX IF EXISTS log_poller_filters_hash_key;

ALTER TABLE evm.log_poller_filters
DROP COLUMN topic2,
DROP COLUMN topic3,
DROP COLUMN topic4,
DROP COLUMN max_logs_kept,
DROP COLUMN logs_per_block;

DROP FUNCTION IF EXISTS evm.f_log_poller_filter_hash(TEXT, NUMERIC, BYTEA, BYTEA, BYTEA, BYTEA, BYTEA);

0 comments on commit e9a2dd0

Please sign in to comment.