Skip to content

Commit 9d0352a

Browse files
authored
Fix duplicate cast function creation for binary bytea (#3860)
The cast functions (binary -> bytea and bytea -> binary) created in this PR should be encapsulated in a DO...BEGIN...END block to prevent duplicate cast function creation issues, like this: This encapsulation ensures the cast functions are created only once and avoids any duplicate function creation errors during multiple executions. Signed-off-by: Herambh Shah <herambhs@amazon.com>
1 parent edad3e7 commit 9d0352a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

contrib/babelfishpg_common/sql/upgrades/babelfish_common_helper--5.2.0--5.3.0.sql

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,29 @@ RETURNS sys.BBF_BINARY
1313
AS 'babelfishpg_common', 'byteabinary'
1414
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
1515

16-
CREATE CAST (pg_catalog.BYTEA AS sys.BBF_BINARY)
17-
WITH FUNCTION sys.byteabinary(pg_catalog.BYTEA, integer, boolean) AS ASSIGNMENT;
16+
DO $$
17+
BEGIN
18+
CREATE CAST (pg_catalog.BYTEA AS sys.BBF_BINARY)
19+
WITH FUNCTION sys.byteabinary(pg_catalog.BYTEA, integer, boolean) AS ASSIGNMENT;
20+
EXCEPTION WHEN duplicate_object THEN
21+
-- Silently ignore if cast already exists
22+
END;
23+
$$;
1824

1925
-- casting from binary to bytea
2026
CREATE OR REPLACE FUNCTION sys.binarybytea(sys.BBF_BINARY, integer, boolean)
2127
RETURNS pg_catalog.BYTEA
2228
AS 'babelfishpg_common', 'binarybytea'
2329
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
2430

25-
CREATE CAST (sys.BBF_BINARY AS pg_catalog.BYTEA)
26-
WITH FUNCTION sys.binarybytea(sys.BBF_BINARY, integer, boolean) AS ASSIGNMENT;
31+
DO $$
32+
BEGIN
33+
CREATE CAST (sys.BBF_BINARY AS pg_catalog.BYTEA)
34+
WITH FUNCTION sys.binarybytea(sys.BBF_BINARY, integer, boolean) AS ASSIGNMENT;
35+
EXCEPTION WHEN duplicate_object THEN
36+
-- Silently ignore if cast already exists
37+
END;
38+
$$;
2739

2840
-- Operator class for numeric_ops to incorporate various operator between numeric and int4 for Index scan
2941
DO $$

0 commit comments

Comments
 (0)