检查Postgres(plpgsql)中是否存在序列

问题描述:

我正在尝试在存储过程中测试序列是否已经存在.

I'm trying to test, within a stored procedure, whether a sequence already exists.

IF EXISTS SEQUENCE seq_name
    RAISE EXCEPTION 'sequence % already exists!', seq_name
END IF;

我尝试了上面几段代码的几种变体,但没有带来好运.我一定给Google加上了错误的字眼,因为我似乎找不到关于此主题的任何内容.感谢您的帮助!

I have tried several variations of the snippet above without luck. I must be giving Google the wrong terms because I can't seem to find anything on the topic. Any help is appreciated!

您应该能够查询pg_class表以查看relname是否存在.

You should be able query the pg_class table to see if the relname exists.

IF EXISTS (SELECT 0 FROM pg_class where relname = '<my sequence name here>' )
THEN
  --stuff here
END IF;