Don't override postgres C symbols with our own (#6300)

When introducing our overrides of pg_cancel_backend and
pg_terminate_backend we accidentally did that in such a way that we
cannot call the original pg_cancel_backend and pg_terminate_backend from
C anymore. This happened because we defined the exact same symbols in
our shared library as postgres does in its own binary.

This fixes that by using a different names for the C function than for
the SQL function.

Making this work in all upgrade and downgrade scenarios is not trivial
though, because we actually need to remove the C function definition.
Postgres errors in two different times when the symbol that a C function
wants to call is not defined in the library it expects it in:
1. When creating the SQL function definition
2. When calling the SQL function

Item 1 causes an issue when creating our extension for the first time.
We then go execute all the migrations that we have. So if the 11.0
migration contains a SQL function definition that still references the
pg_cancel_backend symbol, that migration will fail. This issue is solved
by actually changing the SQL definition in the old migration.

This is not enough to fix all issues though. Item 2 causes an issue
after an upgrade to 11.1, because it won't have the new definition of
the SQL function. This is solved by recreating the SQL functions in the
migration to 11.1. That way it gets the new definition.

Then finally there's the case of downgrades. To continue to make our
pg_cancel_backend SQL function work after downgrading, we will need to
make a patch release for 11.0 that includes the new citus_cancel_backend
symbol. This is done in a separate commit.
pull/6256/head
Jelte Fennema 2022-09-07 11:27:05 +02:00 committed by GitHub
parent d7404a9446
commit e29db74a19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 8 deletions

View File

@ -22,8 +22,8 @@
static bool CitusSignalBackend(uint64 globalPID, uint64 timeout, int sig); static bool CitusSignalBackend(uint64 globalPID, uint64 timeout, int sig);
PG_FUNCTION_INFO_V1(pg_cancel_backend); PG_FUNCTION_INFO_V1(citus_cancel_backend);
PG_FUNCTION_INFO_V1(pg_terminate_backend); PG_FUNCTION_INFO_V1(citus_terminate_backend);
/* /*
* pg_cancel_backend overrides the Postgres' pg_cancel_backend to cancel * pg_cancel_backend overrides the Postgres' pg_cancel_backend to cancel
@ -34,7 +34,7 @@ PG_FUNCTION_INFO_V1(pg_terminate_backend);
* pg_cancel_backend function is used. * pg_cancel_backend function is used.
*/ */
Datum Datum
pg_cancel_backend(PG_FUNCTION_ARGS) citus_cancel_backend(PG_FUNCTION_ARGS)
{ {
CheckCitusVersion(ERROR); CheckCitusVersion(ERROR);
@ -57,7 +57,7 @@ pg_cancel_backend(PG_FUNCTION_ARGS)
* pg_terminate_backend function is used. * pg_terminate_backend function is used.
*/ */
Datum Datum
pg_terminate_backend(PG_FUNCTION_ARGS) citus_terminate_backend(PG_FUNCTION_ARGS)
{ {
CheckCitusVersion(ERROR); CheckCitusVersion(ERROR);

View File

@ -105,3 +105,8 @@ GRANT SELECT ON pg_catalog.pg_dist_operationid_seq TO public;
CREATE SEQUENCE citus.pg_dist_cleanup_recordid_seq; CREATE SEQUENCE citus.pg_dist_cleanup_recordid_seq;
ALTER SEQUENCE citus.pg_dist_cleanup_recordid_seq SET SCHEMA pg_catalog; ALTER SEQUENCE citus.pg_dist_cleanup_recordid_seq SET SCHEMA pg_catalog;
GRANT SELECT ON pg_catalog.pg_dist_cleanup_recordid_seq TO public; GRANT SELECT ON pg_catalog.pg_dist_cleanup_recordid_seq TO public;
-- We recreate these two UDF from 11.0-1 on purpose, because we changed their
-- old definition. By recreating it here upgrades also pick up the new changes.
#include "udfs/pg_cancel_backend/11.0-1.sql"
#include "udfs/pg_terminate_backend/11.0-1.sql"

View File

@ -3,7 +3,7 @@ DROP FUNCTION IF EXISTS pg_catalog.pg_cancel_backend(global_pid bigint) CASCADE;
CREATE OR REPLACE FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint) CREATE OR REPLACE FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint)
RETURNS BOOL RETURNS BOOL
LANGUAGE C LANGUAGE C
AS 'MODULE_PATHNAME', $$pg_cancel_backend$$; AS 'MODULE_PATHNAME', $$citus_cancel_backend$$;
COMMENT ON FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint) COMMENT ON FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint)
IS 'cancels a Citus query which might be on any node in the Citus cluster'; IS 'cancels a Citus query which might be on any node in the Citus cluster';

View File

@ -3,7 +3,7 @@ DROP FUNCTION IF EXISTS pg_catalog.pg_cancel_backend(global_pid bigint) CASCADE;
CREATE OR REPLACE FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint) CREATE OR REPLACE FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint)
RETURNS BOOL RETURNS BOOL
LANGUAGE C LANGUAGE C
AS 'MODULE_PATHNAME', $$pg_cancel_backend$$; AS 'MODULE_PATHNAME', $$citus_cancel_backend$$;
COMMENT ON FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint) COMMENT ON FUNCTION pg_catalog.pg_cancel_backend(global_pid bigint)
IS 'cancels a Citus query which might be on any node in the Citus cluster'; IS 'cancels a Citus query which might be on any node in the Citus cluster';

View File

@ -3,7 +3,7 @@ DROP FUNCTION IF EXISTS pg_catalog.pg_terminate_backend(global_pid bigint, timeo
CREATE OR REPLACE FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint DEFAULT 0) CREATE OR REPLACE FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint DEFAULT 0)
RETURNS BOOL RETURNS BOOL
LANGUAGE C LANGUAGE C
AS 'MODULE_PATHNAME', $$pg_terminate_backend$$; AS 'MODULE_PATHNAME', $$citus_terminate_backend$$;
COMMENT ON FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint) COMMENT ON FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint)
IS 'terminates a Citus query which might be on any node in the Citus cluster'; IS 'terminates a Citus query which might be on any node in the Citus cluster';

View File

@ -3,7 +3,7 @@ DROP FUNCTION IF EXISTS pg_catalog.pg_terminate_backend(global_pid bigint, timeo
CREATE OR REPLACE FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint DEFAULT 0) CREATE OR REPLACE FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint DEFAULT 0)
RETURNS BOOL RETURNS BOOL
LANGUAGE C LANGUAGE C
AS 'MODULE_PATHNAME', $$pg_terminate_backend$$; AS 'MODULE_PATHNAME', $$citus_terminate_backend$$;
COMMENT ON FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint) COMMENT ON FUNCTION pg_catalog.pg_terminate_backend(global_pid bigint, timeout bigint)
IS 'terminates a Citus query which might be on any node in the Citus cluster'; IS 'terminates a Citus query which might be on any node in the Citus cluster';