Search
 
SCRIPT & CODE EXAMPLE
 

SQL

postgres automatic partioning a table

alter table myTable rename to myTable_old;

create table myTable_master(
    forDate date not null,
    key2 int not null,
    value int not null
) partition by range (forDate);
Comment

postgres automatic partioning a table

create function createPartitionIfNotExists(forDate date) returns void
as $body$
declare monthStart date := date_trunc('month', forDate);
    declare monthEndExclusive date := monthStart + interval '1 month';
    -- We infer the name of the table from the date that it should contain
    -- E.g. a date in June 2005 should be int the table mytable_200506:
    declare tableName text := 'mytable_' || to_char(forDate, 'YYYYmm');
begin
    -- Check if the table we need for the supplied date exists.
    -- If it does not exist...:
    if to_regclass(tableName) is null then
        -- Generate a new table that acts as a partition for mytable:
        execute format('create table %I partition of myTable_master for values from (%L) to (%L)', tableName, monthStart, monthEndExclusive);
        -- Unfortunatelly Postgres forces us to define index for each table individually:
        execute format('create unique index on %I (forDate, key2)', tableName);
    end if;
end;
$body$ language plpgsql;
Comment

postgres automatic partioning a table

create or replace view myTable as select * from myTable_master;
Comment

postgres automatic partioning a table

create or replace rule autoCall_createPartitionIfNotExists as on insert
    to myTable
    do instead (
        select createPartitionIfNotExists(NEW.forDate);
        insert into myTable_master (forDate, key2, value) values (NEW.forDate, NEW.key2, NEW.value)
    );
Comment

postgres automatic partioning a table

-- Finally copy the data to our new partitioned table
insert into myTable (forDate, key2, value) select * from myTable_old;

-- And get rid of the old table
drop table myTable_old;
Comment

PREVIOUS NEXT
Code Example
Sql :: amount of entries in a table psql 
Sql :: how to know which table has more data oracle sql 
Sql :: providername system.data. mysql 
Sql :: select function with the column name we want to select 
Sql :: drop unique constraint 
Sql :: IS THEre any difference between using default and := in plsql 
Sql :: how to put value in parameters in mysqldataadapter 
Sql :: unpdate pl sql 
Sql :: how to create an SQL save method in ruby 
Sql :: oracle test if 0 
Sql :: sparql comment multiline 
Sql :: mysql query to add hours to column in table 
Sql :: mysql set user password for a range of ips 
Sql :: sqlite timer 
Sql :: alling a function from PL/SQL in a select statement in ORACLE 
Sql :: sql menampilkan data tabel 
Sql :: restarting of postgresql server when not connecting to default port 
Sql :: online t-sql editor 
Sql :: dynamic where clause in sql server stored procedure 
Sql :: $nbreLivres= mysqli_num_rows($result); $all = mysqli_fetch_all($result,MYSQLI_ASSOC); mysqli_free_result($result) mysqli_close($conn); 
Sql :: check mysql password with docker container magento 2 
Sql :: sql group by and having 
Sql :: check constraint is violated 
Sql :: SQL Equal to Operator (=) 
Sql :: ALTER TABLE myTable RENAME CONSTRAINT PK_constraint to PK01_Constraint; 
Sql :: ring SQLite sqlite_close 
Sql :: Duplix print in Smartforms 
Sql :: mysql case sensitive 
Sql :: ORACLE RANGE DATE USING CONNECT BY PER WEEK,MONTHS,ETC 
Sql :: which is the order of precedence among following operator IN OUT AND OR in sql 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =