Skip to content

Commit

Permalink
Add lock-free ensure that column doesn't exist (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
InverseIntegral authored Jan 29, 2025
1 parent 9f1c619 commit bfd2a10
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/oss/postgres/prepare/database/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ BEGIN
END
$func$;

-- fn_ensure_column_not_exists is a lock-friendly replacement for `ALTER TABLE ... DROP COLUMN IF EXISTS`.
-- WARNING: This function translates all names into lowercase (as plain postgres would).
-- If you want to use lowercase characters, (e.g. through quotation) do not use this funtion.
--
-- Example usage:
--
-- SELECT fn_ensure_column_not_exists('testtable', 'CreatedAt');
CREATE OR REPLACE FUNCTION fn_ensure_column_not_exists(tname TEXT, cname TEXT)
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = LOWER(tname) AND column_name = LOWER(cname)
) THEN
EXECUTE 'ALTER TABLE ' || tname || ' DROP COLUMN IF EXISTS ' || cname || ';';
END IF;
END
$func$;

-- fn_ensure_column_not_null is a lock-friendly replacement for `ALTER TABLE ... ALTER COLUMN ... SET NOT NULL`.
-- WARNING: This function translates all names into lowercase (as plain postgres would).
-- If you want to use lowercase characters, (e.g. through quotation) do not use this funtion.
Expand Down

0 comments on commit bfd2a10

Please sign in to comment.