From: Jim Nasby <jim.nasby@gmail.com>
To: arun chirappurath <arunsnmimt@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: postgres sql assistance
Date: Tue, 16 Jan 2024 14:27:58 -0600
Message-ID: <c6ae555f-52e4-4e41-b863-b2a18f0e8792@gmail.com> (raw)
In-Reply-To: <CAA23Sdtc_hZ3JL+rsm0=j-USGDjMr3hNMExy8KgCGheJ4hokfw@mail.gmail.com>
References: <CAA23Sdtc_hZ3JL+rsm0=j-USGDjMr3hNMExy8KgCGheJ4hokfw@mail.gmail.com>
On 1/16/24 6:34 AM, arun chirappurath wrote:
> I am trying to load data from the temp table to the main table and catch
> the exceptions inside another table.
I don't have a specific answer, but do have a few comments:
- There are much easier ways to do this kind of data load. Search for
"postgres data loader" on google.
- When you're building your dynamic SQL you almost certainly should have
some kind of ORDER BY on the queries pulling data from
information_schema. SQL never mandates data ordering except when you
specifically use ORDER BY, so the fact that your fields are lining up
right now is pure luck.
- EXCEPTION WHEN others is kinda dangerous, because it traps *all*
errors. It's much safer to find the exact error code. An easy way to do
that in psql is \errverbose [1]. In this particular case that might not
work well since there's a bunch of different errors you could get that
are directly related to a bad row of data. BUT, there's also a bunch of
errors you could get that have nothing whatsoever to do with the data
you're trying to load (like if there's a bug in your code that's
building the INSERT statement).
- You should look at the other details you can get via GET STACKED
DIAGNOSTICS [2]. As far as I can tell, your script as-written will
always return the first column in the target table. Instead you should
use COLUMN_NAME. Note that not every error will set that though.
1:
https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-META-COMMAND-ERRVERBOSE
2:
https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-EXCEPTION-DIAGNOSTIC...
--
Jim Nasby, Data Architect, Austin TX
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: jim.nasby@gmail.com, arunsnmimt@gmail.com
Subject: Re: postgres sql assistance
In-Reply-To: <c6ae555f-52e4-4e41-b863-b2a18f0e8792@gmail.com>
References: <CAA23Sdtc_hZ3JL+rsm0=j-USGDjMr3hNMExy8KgCGheJ4hokfw@mail.gmail.com> <c6ae555f-52e4-4e41-b863-b2a18f0e8792@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox