Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tK1Fb-006N1I-7y for pgsql-general@arkaria.postgresql.org; Sat, 07 Dec 2024 20:18:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tK1FY-00Gp1P-E0 for pgsql-general@arkaria.postgresql.org; Sat, 07 Dec 2024 20:18:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tK1FY-00Gp1H-2Y for pgsql-general@lists.postgresql.org; Sat, 07 Dec 2024 20:18:01 +0000 Received: from mail-yw1-x112f.google.com ([2607:f8b0:4864:20::112f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tK1FW-001YLr-B6 for pgsql-general@lists.postgresql.org; Sat, 07 Dec 2024 20:17:59 +0000 Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-6ef66fd2183so29318447b3.1 for ; Sat, 07 Dec 2024 12:17:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733602677; x=1734207477; darn=lists.postgresql.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HAvvUO5DPkYXgFADF7hSW8k0iYGRnb2YIuob9EGjmUM=; b=UnEVm0k8Bp18y8rcrH4VAGTk8RCWA+pGba22V7ZRS6VjB09nRlQgCcv4FSKo21atLW QNNRXSQCF06MicOSzEmmUsczmfM6m/y1HNxRl3ddLXaQ4URvEXvYr5kqzgybu4fsnWbn PrwkMF/tf1nCwxgreuIkp5Rr3TAqdWjkbBnlC1gn09HWTtr96MEKv8xby7gBgFPa7HZ6 zGnvpeRvjkFkYVyEz3iOWRO7u2JxxqfMh26NkN8iBtCZruroqHi2FCVV149zYb7/YC2B WEw1JG1+zYXwg7PyUCRBPxhL3AFT8k7XpSzMOcc4krjjPKXhdF+7mevsrjdeMsuNWS3J k7Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733602677; x=1734207477; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HAvvUO5DPkYXgFADF7hSW8k0iYGRnb2YIuob9EGjmUM=; b=Tf+HI3aitACtJMO3mYm9FxRhDOtPs0NZtF0o63I1L7/ELi/9Y7ihBdF6qsxGw3h2rW lrXrf/zQTUoIzW9QyWXkLiqtZS2jonDCKezd7LvQkD32of3497hc/F3gGWQidVLUcf5T zUhALkcFZhWG51pj9/FmAolbV4dxKc7Yl+DH2Ub+NQ2MZJIhb4ZZ7mXua74ar9FV6IYz I31u2evMAQyiOqIaW7exBDwBm2rerWy/VlyCWRLWx5nH4R1z9tZSIjNByB0yFkkcJwcb Xpe5DIoBbCZIyaH2CkVfVhkHq70smyggA65/YB5cSaTrIasc7aRNf+20rQh5sLfzZbbk AIUg== X-Forwarded-Encrypted: i=1; AJvYcCVSY+yTUxLShYQzJQ7VD9yioMymAapkzYtguUPNwp/yDUyfHdBH9i3hctzrYh63MWeERf48IVwzbmxwyo/h@lists.postgresql.org X-Gm-Message-State: AOJu0YzqH0A67YoeC4AY+kalXPStpwtS+kK9a6fRMhFyGrj2aDCPBa2M 82xtdz6tcXXsClVJJj447WMGNtYR1r3yca8MzsbrcVT7DzWy95/DP1IKGD8vVf4wPsKxrWW4Xqw xpDkSeDeRWH7dDZWOYiLJrQqAXKk= X-Gm-Gg: ASbGncvaZbAC2LK5D8pXoC14qpgqXGj1KONmhdbkwukLmutM/TLHG2nioz5g+HAoNWl zsbTh5Shq/miOzCsBrE3YI78cnr77o5gu/9PCh/XSXex4VFY1vrA+dW74LzwiBeqp X-Google-Smtp-Source: AGHT+IHXkoC9UjYQpcfaZVF/bJ7V3Be/xAs3zl0kBuEDS7Fy3yn4qW2vUr/cyJNENrFvknfITFHscfzsfbys28TtIes= X-Received: by 2002:a05:690c:4a0c:b0:6ef:6035:82b4 with SMTP id 00721157ae682-6efe3c89e9dmr88588447b3.35.1733602677244; Sat, 07 Dec 2024 12:17:57 -0800 (PST) MIME-Version: 1.0 References: <8770b693-0c44-40de-b883-36cc5e718a0a@aklaver.com> In-Reply-To: From: Igor Korot Date: Sat, 7 Dec 2024 14:17:35 -0600 Message-ID: Subject: Re: Insert records in the tavke only if they are not exist To: Adrian Klaver , "pgsql-generallists.postgresql.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, Adrian, On Sat, Dec 7, 2024 at 12:32=E2=80=AFPM Adrian Klaver wrote: > > On 12/7/24 09:59, Igor Korot wrote: > > Hi, Adrian, > > > > On Sat, Dec 7, 2024 at 11:18=E2=80=AFAM Adrian Klaver wrote: > >> > >> On 12/6/24 19:36, Igor Korot wrote: > >>> Hi, All, > >>> > >>> When my application starts up, ot creates some tables and insert reco= rds > >>> 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 differe= nt > >>> users? > >> Is it possible to have the app start up without external users connect= ing? > > > > 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 stat= ing > >>> 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 >