<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Chavan</title>
    <description>The latest articles on DEV Community by Aditya Chavan (@aditya_chavan_5a1cb1adc96).</description>
    <link>https://dev.to/aditya_chavan_5a1cb1adc96</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1142022%2F6c32c1de-9b13-41a1-960c-11b9faaa6b4c.png</url>
      <title>DEV Community: Aditya Chavan</title>
      <link>https://dev.to/aditya_chavan_5a1cb1adc96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya_chavan_5a1cb1adc96"/>
    <language>en</language>
    <item>
      <title>How to implement case insensitivity in PostgreSQL queries</title>
      <dc:creator>Aditya Chavan</dc:creator>
      <pubDate>Thu, 04 Jan 2024 15:54:09 +0000</pubDate>
      <link>https://dev.to/aditya_chavan_5a1cb1adc96/how-to-implement-case-insensitivity-in-postgresql-queries-2icj</link>
      <guid>https://dev.to/aditya_chavan_5a1cb1adc96/how-to-implement-case-insensitivity-in-postgresql-queries-2icj</guid>
      <description>&lt;p&gt;🚀 Must-read for PostgreSQL enthusiasts!&lt;br&gt;
&lt;a href="https://link.medium.com/8LwsRFaz5Fb"&gt;https://link.medium.com/8LwsRFaz5Fb&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>techtips</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to make postgresql queries case insensitive</title>
      <dc:creator>Aditya Chavan</dc:creator>
      <pubDate>Sat, 19 Aug 2023 15:58:54 +0000</pubDate>
      <link>https://dev.to/aditya_chavan_5a1cb1adc96/how-to-make-postgresql-queries-case-insensitive-lpl</link>
      <guid>https://dev.to/aditya_chavan_5a1cb1adc96/how-to-make-postgresql-queries-case-insensitive-lpl</guid>
      <description>&lt;p&gt;Scenarios when we want postgresql to act as case insensitive. We can convert columns to uppercase or lowercase using upper()/lower() to make comparisons case insensitive.&lt;br&gt;
Scenarios where we would need to make such changes -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;where conditions&lt;/li&gt;
&lt;li&gt;Join conditions&lt;/li&gt;
&lt;li&gt;group by — here if put lower/upper on column then that column in select statement should have either lower/upper or other aggregate like max.&lt;/li&gt;
&lt;li&gt;order by columns&lt;/li&gt;
&lt;li&gt;having conditions&lt;/li&gt;
&lt;li&gt;ilike to make like case insensitive&lt;/li&gt;
&lt;li&gt;instead of select distinct lower(col1) from… use select distinct on (lower(col1)) col1 from… as it won’t convert value to lower case while being case insensitive.&lt;/li&gt;
&lt;li&gt;unique constraints are case sensitive. if using on conflict clause in insert queries then it would be case sensitive. the only solution is to remove the conflict and add extra where condition of “AND NOT EXISTS ( select 1 from … where unique constriants columns wrapped by lower())”. If on conflict () update then add update query before the insert query with extra conditions of lower() wrapped the unique constraint columns and “NOT EXISTS …” in the afterward insert query.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>Changes in procedures/functions while migrating from MySQL to PostgreSQL</title>
      <dc:creator>Aditya Chavan</dc:creator>
      <pubDate>Sat, 19 Aug 2023 15:42:45 +0000</pubDate>
      <link>https://dev.to/aditya_chavan_5a1cb1adc96/changes-in-proceduresfunctions-while-migrating-from-mysql-to-postgresql-2ad9</link>
      <guid>https://dev.to/aditya_chavan_5a1cb1adc96/changes-in-proceduresfunctions-while-migrating-from-mysql-to-postgresql-2ad9</guid>
      <description>&lt;p&gt;So, I have listed some of the changes which are needed when migrating our procedures or functions in MySQL to PostgreSQL.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“ ( double quote ) in postgresql don’t signify text but are used to name case sensitive names of columns, tables, etc. as those are case insensitive in postgresql. Instead ‘ ( single quote ) is used for text.
for example.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select age from table1 where name = 'harry'; ( postgresql )
select age from table1 where name = "harry"; ( mysql )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;common json changes from MySQL to Postgresql include :
json_length MySQL = json_array_length
convert text variable to json using ::json
json_extract = -&amp;gt;&amp;gt; operator
json_unquote = trim(‘“’ from abc)
we also have json datatype in postgresql&lt;/li&gt;
&lt;li&gt;if we are using escape characters in string like \n, \t, etc. “E” should be applied before the string like
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E'\t hello \n World'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;FIND_IN_SET(value, array) is replaced with
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;value = ANY(string_to_array(array, ',')::BIGINT[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;“insert ignore” is replaced with “on conflict do nothing”. we can also specify unique columns separately after the conflict in () but not including means all unique constraints are included. Similarly, on duplicate key update is handled by on conflict do update set. EXCLUDED point to the new row for inserting.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert into t1(s1,s2)
select a,b from t2
on conflict do nothing;

insert into t1(s1,s2)
select a,b from t2
on conflict (any unique columns/columns ) do update set
   s1 = EXCLUDED.a,
   s2 = EXCLUDED.b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In mysql cursor we declare continue handler with a variable and have a condition where if the variable is equal to 1 we leave the loop&lt;br&gt;
But in postgresql both are not supported and the only change is to attach statement “exit when not found;” after fetching from the cursor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;loops in cursors can’t have names in postgresql&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FOUND_ROWS() in replaced by ROW_COUNT.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get diagnostics count = ROW_COUNT;
concat('Inserted records are ', count) into var_output;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;for exceptions — declare handler is not supported in postgresql but can use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare
begin
    statements;
exception
    when condition [or condition...] then
       handle_exception;
   [when condition [or condition...] then
       handle_exception;]
   [when others then
       handle_other_exceptions;
   ]
end;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In delete statement we can delete from the source table only ( unless cascade delete is applied on child tables ). Also, 1st “JOIN” keyword after table is replaced with “USING”. The joining columns of the source table need to be specified in the where clause.
for eg
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete from abc 
using def
join efg on def.name = efg.name
where abc.id = def.objectid and abc.col1 = efg.col1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;enum support in postgresql is not provided like in MySQL. All enum columns should be declared as text or varchar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In update statements, we can update the source table only because table prefix for columns is removed in postgresql. Also, 1st “JOIN” keyword after table is replaced with “FROM”. The joining columns of the source table need to be specified in the where clause. set clause is before the from clause. for eg&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update abc a
set 
  col1 = o.name
  col2 = w.type
from def o
join fgh w on o.col3 = w.col3
where 
  a.addressid = o.id
and a.contactid = w.id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;instr( string, sub_string ) is replaced by POSITION(sub_string IN string).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While creating table instead of NOT NULL AUTO_INCREMENT use GENERATED ALWAYS AS IDENTITY&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cannot create index while creating table in postgresql, need to create index explicitly in new SQL statement. Also, cannot create index on variable length of column like col(255)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DROP TEMPORARY TABLE replaced by DROP TABLE. works for both types.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;group_concat = string_agg(string, ‘separator’)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Information_Schema.columns, COLUMN_TYPE is replaced by DATA_TYPE&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;unique constrainst are created using unique indexes. So, first need the index to create the constraint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CURDATE() is replaced with CURRENT_DATE&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DATE_ADD() is replaced with date_var + anything. [ if anything is integer then days are added if anything is INTERVAL ‘1 WEEK’ then 1 week is added ]&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>mysql</category>
      <category>postgres</category>
      <category>procedures</category>
      <category>migration</category>
    </item>
  </channel>
</rss>
