---
name: "table_dispatcher"
section: "1"
platform: "linux"
package: "skytools"
version: "2.1.13-2"
release: "Debian 7 (Wheezy)"
platforms: ["linux"]
url: "https://new.carta.tech/table_dispatcher"
---

# table\_dispatcher(1)

PgQ consumer that is used to write source records into partitoned table.

## NAME

table\_dispatcher - PgQ consumer that is used to write source records into partitoned table.

## SYNOPSIS

```
table_dispatcher.py [switches] config.ini
```

## DESCRIPTION

table\_dispatcher is PgQ consumer that reads url encoded records from source queue and writes them into partitioned tables according to configuration file. Used to partiton data. For example change log’s that need to kept online only shortly can be written to daily tables and then dropped as they become irrelevant. Also allows to select which columns have to be written into target database Creates target tables according to configuration file as needed.

## QUICK-START

Basic table\_dispatcher setup and usage can be summarized by the following steps:

 1\. PgQ must be installed in source database. See pgqadm man page for details. Target database must have pgq\_ext schema installed.

 2\. edit a table\_dispatcher configuration file, say table\_dispatcher\_sample.ini

 3\. create source queue

```
$ pgqadm.py ticker.ini create <queue>
```

 4\. launch table dispatcher in daemon mode

```
$ table_dispatcher.py table_dispatcher_sample.ini -d
```

 5\. start producing events

## CONFIG

### Common configuration parameters

job\_name

Name for particulat job the script does. Script will log under this name to logdb/logserver. The name is also used as default for PgQ consumer name. It should be unique.

pidfile

Location for pid file. If not given, script is disallowed to daemonize.

logfile

Location for log file.

loop\_delay

If continuisly running process, how long to sleep after each work loop, in seconds. Default: 1.

connection\_lifetime

Close and reconnect older database connections.

log\_count

Number of log files to keep. Default: 3

log\_size

Max size for one log file. File is rotated if max size is reached. Default: 10485760 (10M)

use\_skylog

If set, search for \[./skylog.ini, ~/.skylog.ini, /etc/skylog.ini\]. If found then the file is used as config file for Pythons logging module. It allows setting up fully customizable logging setup.

### Common PgQ consumer parameters

pgq\_queue\_name

Queue name to attach to. No default.

pgq\_consumer\_id

Consumers ID to use when registering. Default: %(job\_name)s

### table\_dispatcher parameters

src\_db

Source database.

dst\_db

Target database.

dest\_table

Where to put data. when partitioning, will be used as base name

part\_field

date field with will be used for partitioning.

part\_template

SQL code used to create partition tables. Various magic replacements are done there:

\_PKEY

comma separated list of primery key columns.

\_PARENT

schema-qualified parent table name.

\_DEST\_TABLE

schema-qualified partition table.

\_SCHEMA\_TABLE

same as *DEST\_TABLE but dots replaced with "\_*", to allow use as index names.

### Example config

```
[table_dispatcher]
job_name          = table_dispatcher_source_table_targetdb
```

```
src_db            = dbname=sourcedb
dst_db            = dbname=targetdb
```

```
pgq_queue_name    = sourceq
```

```
logfile           = log/%(job_name)s.log
pidfile           = pid/%(job_name)s.pid
```

```
# where to put data.  when partitioning, will be used as base name
dest_table        = orders
```

```
# names of the fields that must be read from source records
fields            = id, order_date, customer_name
```

```
# date field with will be used for partitioning
part_field        = order_date
```

```
# template used for creating partition tables
part_template     =
     create table _DEST_TABLE () inherits (orders);
     alter table only _DEST_TABLE add constraint _DEST_TABLE_pkey primary key (id);
     grant select on _DEST_TABLE to group reporting;
```

## COMMAND LINE SWITCHES

Following switches are common to all skytools.DBScript-based Python programs.

\-h, --help

show help message and exit

\-q, --quiet

make program silent

\-v, --verbose

make program more verbose

\-d, --daemon

make program go background

Following switches are used to control already running process. The pidfile is read from config then signal is sent to process id specified there.

\-r, --reload

reload config (send SIGHUP)

\-s, --stop

stop program safely (send SIGINT)

\-k, --kill

kill program immidiately (send SIGTERM)

## LOGUTRIGA EVENT FORMAT

PgQ trigger function pgq.logutriga() sends table change event into queue in following format:

ev\_type

(op \|\| ":" \|\| pkey\_fields). Where op is either "I", "U" or "D", corresponging to insert, update or delete. And pkey\_fields is comma-separated list of primary key fields for table. Operation type is always present but pkey\_fields list can be empty, if table has no primary keys. Example: I:col1,col2

ev\_data

Urlencoded record of data. It uses db-specific urlecoding where existence of *=* is meaningful - missing *=* means NULL, present *=* means literal value. Example: id=3&name=str&nullvalue&emptyvalue=

ev\_extra1

Fully qualified table name.
