I want to create a partition table month wise only 12 tables for multiple years - postgresql

I am new to the partitioning of a table and I want to make a partition of the table by range type on the inserted_on column in this table the records are inserted around ~ 40000 daily
I have tried creating a partition table as:
CREATE TABLE My.table_name_fy2022_01 PARTITION OF My.table_name FOR VALUES FROM ('2022-01-01') TO ('2022-02-01');
But this way i will have to create 12 tables per year and that I don't want to do.
My question is:- how to create a partition table such as the no. of partition table be only 12 (months wise) and stores the data according to a specific month's partition.
For Example:-
partition table June
record of 2022-06-20 insert into June,
record of 2023-06-16 insert into June,
record of 2024-06-10 insert into June,
and So on

PARTITION BY HASH should be used like:
PARTITION BY HASH(MONTH(use_time)) PARTITIONS 12;

Related

Postgres: Convert non-partitioned table to partitioned with a bit downtime

I have a non-partitioned table record which is append-only and I intended to partition it by range of created timestamp column using postgres native partition. (one partition per month)
I can tolerate a bit of downtime, so my plan is:
Create new table record_partitioned with partitions; copy all past month’s data into new partitioned table
Stop write into the table, copy current month’s data into new partitioned table (a bit of downtime)
Rename old table as record_archived, and rename new table as record
Resume write into table
Does this make sense?
That should work, but you can also consider the following:
create a new partitioned table
add a partition for the current month
attach the existing large table as a partition for all past data
once all data in the existing table have expired, drop it

Range Partitions by Date with One Partition for NULL dates

I would like to have a table with deleted column containing the date the item was soft-deleted. Rows with NULL value in deleted column are the active ones. I was not able to figure our the syntax to create a partition for null values in deleted column. What is the syntax of creating such column?
create table my_table_pointing(street_id int, p_city_id int, name varchar(10), deleted date)
PARTITION BY RANGE (deleted);
CREATE TABLE my_table_pointing_2020 PARTITION OF my_table_pointing
FOR VALUES FROM ('2020-01-01') TO ('2021-01-01');
CREATE TABLE my_table_pointing_active PARTITION OF my_table_pointing
"for all rows where date is null"...
Thanks!
Provided you are on PG11 or later, you can create a default partition, and rows with deleted is null will be routed there.
create table my_table_pointing_active partition of my_table_pointing default;

POSTGRES: Aggregating full table data in case of table partitions

I'm new to table partitions in Postgres and have one doubt.
Let us assume I have a table:
product_visitors
I can create multiple partitions like:
product_visitors_year_2017
product_visitors_year_2018
etc.
I can create a trigger which can redirect insertion on product_visitors to appropriate table.
My question is, what if I want to aggregate on full data of product_visitors? For example, products and their visit count
As I understand, at the moment, data resides in year wise tables instead of main table
In Postgres 10 inserts will automatically be routed to the correct partition.
If you select from the "base table" product_visitors without any condition limiting the rows to one (or more) specific partitions, Postgres will automatically read the data from all partitions.
So
select count(*)
from product_visitors;
will count the rows in all partitions.

Hive how to load data into partitioned table from unpartitioned table with where condition

samp1 table is a partitioned on the year and month columns
The data is stored in the ORCFile format
Insert the records from January, 2008, of the samp2 table into the
appropriate partition of weather_partitione
d
INSERT OVERWRITE TABLE dest_table_name PARTITION (year, month) SELECT all other columns,year,month FROM source_table_name;
When using dynamic partitions, partition columns should come as the last columns in the select query.

Hive partitioning external table based on range

I want to partition an external table in hive based on range of numbers. Say numbers with 1 to 100 go to one partition. Is it possible to do this in hive?
I am assuming here that you have a table with some records from which you want to load data to an external table which is partitioned by some field say RANGEOFNUMS.
Now, suppose we have a table called testtable with columns name and value. The contents are like
India,1
India,2
India,3
India,3
India,4
India,10
India,11
India,12
India,13
India,14
Now, suppose we have a external table called testext with some columns along with a partition column say, RANGEOFNUMS.
Now you can do one thing,
insert into table testext partition(rangeofnums="your value")
select * from testtable where value>=1 and value<=5;
This way all records from the testtable having value 1 to 5 will come into one partition of the external table.
The scenario is my assumption only. Please comment if this is not the scenario you have.
Achyut