trying to drop tables from log table excluding constraints - postgresql

I have an audit log table that logs all create table statements with timestamp. I can write a script to drop tables older than today but how can i exclude all the constraints that gets logged as well during a create statement. All i need is "john.plots" & "raj.auth" to be able to drop this table.
thanks
ddl_date ddl_tag ID object_name
2022-07-07 16:40:06 CREATE FOREIGN 1 raj.auth
2022-07-07 17:14:33 CREATE TABLE 6 john.plots_source_address_id_seq
2022-07-07 17:14:33 CREATE TABLE 7 john.plots
2022-07-07 17:14:33 CREATE TABLE 8 john.plots_pkey
2022-07-07 17:14:33 CREATE TABLE 9 john.plots_source_address_id_seq

Related

How to create encoded primary key for table from two different table primary keys

I have two table Table 1 and Table 2 now I have to create thrid table whose primary key is encoded and combination of table1 and table 2 pk's. I also want to fetch the record of table3 with table 1 and table 2 on the basis of table 3 pk.
can some one help me.
solution in my mind was if I can create some thing similar to JWT toke from the table 1 and table 2 pk's and store it in table 3 pk. but the problems is how I will decode it and perform basic CRUD operation with join query between different tables

How to fill two related tables via a foreign key in a trigger function?

I have two tables which I want to fill their corresponding FOREIGN KEYs simultaneously through a TRIGGER at the time of inserting data into customers table:
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
sld_id integer,
customer_name varchar(35)
);
CREATE TABLE slds (
sld_id SERIAL PRIMARY KEY,
customer_id integer,
sld_code varchar(8) UNIQUE
);
ALTER TABLE customers
ADD CONSTRAINT customers_sld_id_fk
FOREIGN KEY (sld_id)
REFERENCES slds(sld_id);
ALTER TABLE slds
ADD CONSTRAINT slds_customer_id_fk
FOREIGN KEY(customer_id)
REFERENCES customers(customer_id);
I have tried to use an AFTER INSERT trigger function, but NEW.customer_id returned NULL.
Then I used BEFORE INSERT which got me the value of NEW.customer_id. However, because of the constraint and the fact that the insertion didn't take place yet the FOREIGN KEY CONSTRAINT is not fulfilled and I get an error.
I have read here that currval() and lastval() can be used but not recommended.
So I created a proxy table to store the generated values. Then, an AFTER INSERT trigger to fill in those fields back in the related tables.
I thought of using a CREATE TEMP TABLE, but found out that it only lasts for the duration of the calling function and not the connection session. Maybe I misunderstood the error message.
Is this a normal efficient practice? Namely, having a dirty table around just to use for such situations.
Or maybe there is another way to achieve this without using a proxy table?
EDITED:
SAMPLE DATA
customersTABLE:
customer_id slds_id customer_name
1 1 johns
3 2 jenn
4 3 thomas
7 4 jeff
8 5 robin
9 6 chris
10 7 larry
slds TABLE:
slds_id slds_code customer_id
1 SL747561 1
2 SL710031 3
3 SL719995 4
4 SL765369 7
5 SL738011 8
6 SL722232 9
7 SL751591 10
EDIT 2:
Forgot to mention that slds_code is generated within a trigger function:
sld_code varchar(8) := 'SL7'||to_char(floor(random() * 100000 + 1)::int, 'fm00000');

add postgres partitioning to existing table

All examples show:
CREATE TABLE ... PARTITION BY ...
Which is kind of ridiculous, because the only time you would use partitioning is when a dataset has become too large, which by definition is not going to be a new table. If someone is making a new table with partitioning, i think almost anyone would criticize that as a premature optimization.
Just create a partitioned table and attach the existing table as a partition:
create table test (a int);
insert into test select generate_series(1,10);
alter table test_parent attach partition test DEFAULT;
select * from test_parent;
a
----
1
2
3
4
5
6
7
8
9
10
(10 rows)
You could also rename the table. However, if you do this, you will need to re-define any views that point at the original table.

Attach partition LIST to existing table in postgres 11

I am trying to ALTER a table to use partitions LIST in postgres 11. I have been trying for hours but i keep getting errors.
I have a huge table, clients, with ( client_id, customer_id, value).
I have already created a new empty table, clients, by renaming the old table to clients_old and then created the new table with: CREATE TABLE clients( like clients_old including all).
And from here I am stuck when trying to add the LIST partition.
I have tried to:
ALTER TABLE Clients attach PARTITION BY LIST (client_id) --> fail;
ALTER TABLE Clients attach PARTITION LIST (client_id) --> fail;
ALTER TABLE Clients ADD PARTITION LIST (client_id) --> fail;
What syntax should I use to alter the table to use partitions?
Quote from the manual
It is not possible to turn a regular table into a partitioned table or vice versa
So, you can not change an existing non-partitioned table to a partitioned table.
You need to create a new table (with a different name) that is partitioned, create all necessary partitions and then copy the data from the old table to the new, partitioned table.
Something like:
create table clients_partitioned
(
.... all columns ...
)
PARTITION BY LIST (client_id);
Then create the partitions:
create table clients_1
partition of clients_partioned
for values in (1,2,3);
create table clients_1
partition of clients_partioned
for values in (4,5,6);
Then copy the data:
insert into clients_partitioned
select *
from clients;
Once that is done, you can drop the old table and rename the new table:
drop table clients;
alter table clients_partitioned rename to clients;
Don't forget to re-create your foreign keys and indexes.
I had to add for tag in order to add the partition:
create table clients_1
partition of clients_partioned
for values in (4,5,6);
because without for was a syntax error.

Postgres 10 Partitioning - Parent Table Not Visible

I've upgraded to Postgres 10 and I'm planning to deploy some partitioned tabled using the new declarative partitioning syntax:
-- Create a parent table
create table test(
name text,
created timestamp with time zone default now()
) partition by range(created);
-- Create a partition
create table test_2017 partition of test
for values from('1 JAN 2017') to ('1 JAN 2018');
I'd like to be able to reference the parent table in my functions and queries, but only the partitioned tables seem to get resolved by my tools. In both PhpStorm 2017.3 RC and BigSQL's PGAdmin III schema browsers, the table listings both show the partition, but not the parent table. In PhpStorm's editor, I get an "unresolved table" warning whenever I reference "test", however insert/select queries still work.
Have I missed something, or are the tools not yet fully compatible with partitioning?