Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing COMMENT ON from all objects in PostgreSQL

Tags:

postgresql

ddl

In the same vein as pg_dump without comments on objects?, is anyone aware of a command to quickly get rid of the comments (created with COMMENT ON) on all objects at once ?

For now, I resorted to bash generating a SQL script that would void one by one the comments on each table/view/column, but it is quite slow, especially with >4000 columns.
Example:

COMMENT ON COLUMN table1.column1 IS NULL;
COMMENT ON COLUMN table1.column2 IS NULL;
COMMENT ON COLUMN table1.column3 IS NULL;
...
like image 488
Sébastien Clément Avatar asked Jun 21 '13 14:06

Sébastien Clément


People also ask

How do I uncomment multiple lines in PostgreSQL?

Syntax Using /* and */ symbols In PostgreSQL, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.

How do you delete a comment on Pgadmin?

COMMENT stores a comment about a database object. Only one comment string is stored for each object, so to modify a comment, issue a new COMMENT command for the same object. To remove a comment, write NULL in place of the text string. Comments are automatically dropped when their object is dropped.

How do I see column comments in PostgreSQL?

Descriptions of Tables To view the Description column for the placenames table, run the \d+ command in psql (here's a list of \d commands in psql). Simply running the \d command alone will not show this column so you'll need to add the + to make it visible.

How do I delete everything in PostgreSQL?

First, specify the table from which you want to delete data in the DELETE FROM clause. Second, specify which rows to delete by using the condition in the WHERE clause. The WHERE clause is optional. However, if you omit it, the DELETE statement will delete all rows in the table.


2 Answers

I have faced a very similar problem some time ago and came up with a very simple solution: delete from the system catalog table pg_description directly. Comments are just "attached" to objects and don't interfere otherwise.

DELETE FROM pg_description WHERE description = 'something special';

Disclaimer:
Manipulating catalog tables directly is dangerous and a measure of last resort. You have to know what you are doing and you are doing it at your own risk! If you screw up, you may screw up your database (cluster).

I asked about the idea on pgsql-admin list and got an encouraging answer from Tom Lane:

> DELETE FROM pg_description WHERE description = 'My very special
> totally useless comment.';

> AFAICS, there are no side-effects. Are there any?

It's safe enough, as long as you don't delete the wrong comments.
There's no hidden infrastructure for a comment.

            regards, tom lane

You should make sure that there aren't any comments you'd rather keep. Inspect what your are going to delete first. Be aware that many built-in Postgres objects have comments, too.

For instance, to only delete all comments on table columns, you could use:

SELECT *
-- DELETE
FROM   pg_description
WHERE  objsubid > 0;

The manual informs about the column objsubid:

For a comment on a table column, this is the column number (the objoid and classoid refer to the table itself). For all other object types, this column is zero.

like image 90
Erwin Brandstetter Avatar answered Oct 17 '22 01:10

Erwin Brandstetter


Ok, thanks to your help, I found the following commands pretty useful:

To delete a comment from a given column position of a specific object (here, mytable), you could go:

DELETE FROM pg_description WHERE (SELECT relname FROM pg_class WHERE oid=objoid)='mytable' AND objsubid=2;

...but note that it's not more efficient than using COMMENT ON mytable.myfield IS NULL;

Now, to delete all comments from my user-defined views and underlying columns, here's what works very well:

DELETE FROM pg_description WHERE (SELECT relkind FROM pg_class WHERE oid=objoid)='v' AND (SELECT relname FROM pg_class WHERE oid=objoid) ~ 'v_';

where:

  • (SELECT relkind FROM pg_class WHERE oid=objoid)='v': all views
  • (SELECT relname FROM pg_class WHERE oid=objoid) ~ 'v_' : additional security, my views' names all start with 'v_'
like image 40
Sébastien Clément Avatar answered Oct 17 '22 01:10

Sébastien Clément