Introducing PostgreSQL

PostgreSQL is an advanced, enterprise-class, and open-source relational database system. PostgreSQL supports both SQL and JSON querying and is a highly stable database backed by more than 20 years of development by the open-source community.

Recently, in one of the projects, during the migration of data from XML file to database in PostgreSQL to columns’ null’ was transferred as a string but not DB NULL. As a result, we must update records of column/columns with a particular string value, e.g., ‘null’ to DBNULL of a record in a table without listing every column in a PostgreSQL Database. Around 8 + tables had more than 50+ columns. Updating data of each column to DB Null by writing an update statement for each record was tedious and cumbersome. Came up with a function that will help update records with DB Null by replacing the ‘null’ string.

Although there are system stored procedures that do a “for each database” or a “for each table”, there is not a system stored procedure that does “for each column”. So, trying to find value in any column in database requires us to build the query to look through each column using and/or operator between each column. The code allows us to search for a particular string value in all text data type columns such as (char, nchar, ntext, nvarchar, text and varchar).

Using T-SQL and system tables or system views, I have written query to update records in columns.

Solution

  1.  Run listed in line script to create function in PostgreSQL database.

CREATE OR REPLACE

FUNCTION PUBLIC.FN_CONVERTSTRINGNULL_NULL( _TBL TEXT) RETURNS VOID LANGUAGE ‘PLPGSQL’ COST 100 VOLATILE AS $BODY$

DECLARE

_CURS REFCURSOR;

REC RECORD;
_SQL CHARACTER VARYING := NULL;

BEGIN OPEN _CURS FOR EXECUTE ‘SELECT C.COLUMN_NAME

BEGIN OPEN _CURS FOR EXECUTE ‘SELECT C.COLUMN_NAME
FROM PG_CATALOG.PG_STATIO_ALL_TABLES AS ST
INNER JOIN PG_CATALOG.PG_DESCRIPTION PGD ON (PGD.OBJOID=ST.RELID)
RIGHT OUTER JOIN INFORMATION_SCHEMA.COLUMNS C ON (PGD.OBJSUBID=C.ORDINAL_POSITION AND C.TABLE_SCHEMA=ST.SCHEMANAME AND C.TABLE_NAME=ST.RELNAME)
WHERE TABLE_SCHEMA = ”PUBLIC” AND TABLE_NAME = ”’ || _TBL || ”’
AND C.DATA_TYPE LIKE ”CHARACTER VARYING”’ FOR
UPDATE;

LOOP FETCH NEXT
FROM
_CURS
INTO
REC;
EXIT

WHEN REC IS NULL;

RAISE NOTICE ‘%’,
REC.COLUMN_NAME;

EXECUTE FORMAT(‘UPDATE %I SET ‘ || REC.COLUMN_NAME || ‘= NULL’ || ‘ WHERE ‘ || REC.COLUMN_NAME || ‘=’ || ”’NULL”’, _TBL)
USING REC.COLUMN_NAME;
END LOOP;
END $BODY$;

 Postgre SQL

The function gets created in the PostgreSQL database and it takes one parameters.
table – table name in which we want to replace string.

Purpose: Function to change ‘null’ [string] to DBnull in all columns in a table of PostgreSQL Database.

Listed in line query will search for columns having data type as ”character varying” (char, nvarchar,text etc.) in a table of database and execute statement will update column values to string value. [Eg ”’null”’ which is string replaced by DB NULL [PostgreSQL] ).

SELECT C.COLUMN_NAME
FROM PG_CATALOG.PG_STATIO_ALL_TABLES AS ST
INNER JOIN PG_CATALOG.PG_DESCRIPTION PGD ON (PGD.OBJOID=ST.RELID)
RIGHT OUTER JOIN INFORMATION_SCHEMA.COLUMNS C ON (PGD.OBJSUBID=C.ORDINAL_POSITION
AND C.TABLE_SCHEMA=ST.SCHEMANAME AND C.TABLE_NAME=ST.RELNAME)
WHERE TABLE_SCHEMA = ”PUBLIC” AND TABLE_NAME = ”’||_TBL||”’
AND C.DATA_TYPE LIKE ”CHARACTER VARYING”’

EXECUTE FORMAT (‘UPDATE %I SET ‘||REC.COLUMN_NAME||’= NULL’|| ‘ WHERE
‘||REC.COLUMN_NAME ||’=’||”’NULL”’,_TBL)

 

Sample Example

 

  • Create sample table eg tbl_generic_form_bak for testing purpose.  [sample screen shot listed in line for reference] . Submitterdax, submitteraddrs column have string ‘null’ as value.

 

PostgreSQL

  • After running the function all string ‘null’ are converted to DB NULL:
    select * from public.fn_convertstringnull_NULL(‘tbl_generic_form_bak’)
    Note: Input parameter is table name without schema name [tbl_generic_form_bak)

 

PostgreSQL

  • Depending on the size of database this could take some time to run, so be careful when you run this since it will be hitting every table and every column that has one of these datatypes: char, nchar, nvarchar, varchar, text and ntext.

 

  • Similarly, same SP can be used to change ‘0001-01-01 00:00:00’ [string] to NULL [DBnull] or any other string depending upon requirement.

 

  • Hope this function will help in replacing string value to DB NULL or any other string as required in table of database for all columns.

Postgre users often choose some combination of these deployment models to update columns based upon their team requirements and organizational standards. The choice is less about deciding which model to use and more about which choice to use for a given project.

 

 

Vijay
Vijay
Sr Technical Lead