Re: Insert records in the tavke only if they are not exist - Mailing list pgsql-general

From Igor Korot
Subject Re: Insert records in the tavke only if they are not exist
Date
Msg-id CA+FnnTycFrDX4YRXAaqkijv5dLGCStuDV_nZaBQOe_CHcUKTag@mail.gmail.com
Whole thread Raw
In response to Re: Insert records in the tavke only if they are not exist  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: Insert records in the tavke only if they are not exist
List pgsql-general
Hi, Adrian,

On Sat, Dec 7, 2024 at 12:32 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
>
> On 12/7/24 09:59, Igor Korot wrote:
> > Hi, Adrian,
> >
> > On Sat, Dec 7, 2024 at 11:18 AM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> >>
> >> On 12/6/24 19:36, Igor Korot wrote:
> >>> Hi, All,
> >>>
> >>> When my application starts up, ot creates some tables and insert records
> >>> in them.
> >>>
> >>> When the app starts for the second time it should check if the tables
> >>> and the records in them are exist and skip the process.
> >>
> >> Does this also apply to starts after the second time?
> >
> > Yes, it does.
> >
> > However, if the new table is created in the meantime - the new record
> > should be created, because it is not there yet.
>
> What determines that a new table needs to be created and populated?

Consider the following scenario:

1. Program is installed.
2. Program is started for the first time.
3. My tables are created and populated
4. Program is executed.
5. User closes the program.
6.. Later on the user decides that there is a need
for another table (inside psql or any other client)
7. Then the program starts for the second time.

At this time all my tables that were created will stay (courtesy
of CREATE TABLE IF NOT EXIST).
All records that were there are staying unchanged.
However, for the table that was made in between the runs
will be added

Now if the program is installed on 2 different machines
and started simultaneously on both - I want to ensure that
only 1 set of tables is made and only 1 set of records in them
is available

Now, the creation/population is done inside a transaction.

>
> >
> >>
> >> Is the data expected to change over time?
> >
> > Data change is possible.
> >
> > This is one of the tables:
> >
> >      "CREATE TABLE IF NOT EXISTS \"sys.abcatfmt\"(\"abf_name\" char(30)
> > NOT NULL, \"abf_frmt\" char(254), \"abf_type\" smallint, \"abf_cntr\"
> > integer" ));";
> >      "CREATE UNIQUE INDEX IF NOT EXISTS pbcatf_x ON
> > \"sys.abcatfmt\"(\"abf_name\" ASC);";
> >
> > My understanding is that "INSERT OR IGNORE" will check the unique
>
> It is INSERT ... ON CONFLICT DO NOTHING | UPDATE.
>
> > index and will not do anything if the recrd
> > with such data on the index already exists.

Good.
I'm using INSERT ON CONFLICT DO NOTHING.

>
> So you have the option of either skipping the insert or updating
> selected fields in the row.
>
> >
> >>
> >>>
> >>> Everything is good, except what if I have a connection from 2 different
> >>> users?
> >> Is it possible to have the app start up without external users connecting?
> >
> > Not sure what you mean here.
> >
> > Are you asking if the computer can run the app?
> > Then the answer is NO. App is always started by the user.
> > Please clarify.
>
> This is going to need a more detailed explanation of what 'app' means.
>
> Are you talking about the front end that the user launches or the
> backend that runs the database or something else?

Front-end.
This is a C++ app.

>
>
> >
> > Thank you.
> >
> >>
> >>>
> >>> I can run this inside transaction, but will this be enough? Will stating
> >>> transaction lock the DB and the second user will wait for transaction to
> >>> complete?
> >>>
> >>> Thank you.
> >>>
> >>
> >> --
> >> Adrian Klaver
> >> adrian.klaver@aklaver.com
> >>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>



pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Errors when restoring backup created by pg_dumpall
Next
From: Adrian Klaver
Date:
Subject: Re: Insert records in the tavke only if they are not exist